package admin

import (
	. "enca/module/db"
)

//博客列表
type AdminBlogList struct {
	Id          int    `json:"id,omitempty" form:"id" db:"id"`
	Title       string `json:"title,omitempty" form:"title" db:"title"`
	Content     string `json:"content,omitempty" form:"content" db:"content"`
	ReleaseTime string `json:"releaseTime,omitempty" form:"release_time" db:"release_time"`
	UpdateTime  string `json:"updateTime,omitempty" form:"update_time" db:"update_time"`
	State       string `json:"state,omitempty" form:"state" db:"state"`
	SeeNum      string `json:"seeNum,omitempty" form:"see_num" db:"see_num"`
}

//博客配置
type AdminBlogConfig struct {
	Id    int    `json:"id,omitempty" form:"id" db:"id"`
	Bg    string `json:"bg,omitempty" form:"bg" db:"bg"`
	Title string `json:"title,omitempty" form:"title" db:"title"`
	Descs string `json:"descs,omitempty" form:"descs" db:"descs"`
	Other string `json:"other,omitempty" form:"other" db:"other"`
}

//错误
func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

func BlogList() (code int, data interface{}) {
	var blogList []AdminBlogList

	err := Db.Select(&blogList, "SELECT * FROM blog_list order by release_time desc")
	checkErr(err)

	if len(blogList) > 0 {
		code = 200
		data = blogList
	} else {
		code = 500
		data = "请求失败"
	}

	return
}

func BlogAdd(title string, content string, releaseTime string, updateTime string, state string) (code int, data interface{}) {
	info, err := Db.Prepare(`INSERT blog_list (title, content, release_time, update_time, state, see_num) values (?, ?, ?, ?, ?, 0)`)
	checkErr(err)

	res, ierr := info.Exec(title, content, releaseTime, updateTime, state)
	checkErr(ierr)

	id, rerr := res.LastInsertId()
	checkErr(rerr)

	code = 200

	data = id

	return
}

func BlogDel(id string) (code int, data string) {
	info, _ := Db.Prepare(`DELETE FROM blog_list WHERE id=?`)

	_, err := info.Exec(id)
	checkErr(err)

	code = 200

	data = "已删除"

	return
}

func BlogOnline(id string, state string) (code int, data string) {
	info, _ := Db.Prepare(`UPDATE blog_list SET state=? WHERE id=?`)

	_, err := info.Exec(state, id)
	checkErr(err)

	code = 200

	data = "修改成功"

	return
}

func BlogConfig() (code int, data interface{}) {
	var blogConfig []AdminBlogConfig

	err := Db.Select(&blogConfig, "SELECT * FROM blog_config")
	checkErr(err)

	if len(blogConfig) > 0 {
		code = 200
		data = blogConfig[0]
	} else {
		code = 500
		data = "请求失败"
	}

	return
}

func BlogConfigUpdate(bg string, title string, descs string, other string) (code int, data string) {
	info, _ := Db.Prepare("UPDATE blog_config SET bg=?, title=?, descs=?, other=?")

	_, err := info.Exec(bg, title, descs, other)
	checkErr(err)

	code = 200

	data = "修改成功"

	return
}

func BlogDetail(id string) (code int, data interface{}) {
	var blogDatail []AdminBlogList

	err := Db.Select(&blogDatail, "SELECT * FROM blog_list WHERE id="+id+"")
	checkErr(err)

	if len(blogDatail) > 0 {
		code = 200
		data = blogDatail[0]
	} else {
		code = 500
		data = "请求失败"
	}

	return
}

func BlogEdit(id string, title string, content string, updateTime string) (code int, data string) {
	info, _ := Db.Prepare("UPDATE blog_list SET title=?, content=?, update_time=? WHERE id=?")

	_, err := info.Exec(title, content, updateTime, id)
	checkErr(err)

	code = 200

	data = "修改成功"

	return
}

func BlogDrawingDel(id string) (code int, data string) {
	info, _ := Db.Prepare(`DELETE FROM drawing_list WHERE id=?`)

	_, err := info.Exec(id)
	checkErr(err)

	code = 200

	data = "已删除"

	return
}
